﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCharacter : MonoBehaviour
{
    public SpriteRenderer tank;

    public List<Sprite> SpriteList;

    //0 = up ,1 = left ,2 = dowm ,3=right

    public float MoveSpeed = 200;

    public Vector3 OffsetRotation = Vector3.zero;

    public Vector3 OrignalRotation = new Vector3(0, 0, 90);
    //炮弹
    public GameObject Missle;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        BoxCollider2D box = collision.transform.GetComponent<BoxCollider2D>();
        if (box != null)
        {
            Debug.LogError(box.transform.name);
            Debug.LogError(box.gameObject.name);
        }
    }
    private void OnCollisionStay2D(Collision2D collision)
    {

    }
    private void OnCollisionExit2D(Collision2D collision)
    {

    }
    private void CreateMissile()
    {
        GameObject go = GameObject.Instantiate(this.Missle, this.transform.position,Quaternion.Euler(this.OffsetRotation));
        go.SetActive(true);
    }

    public void MyMethod()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        if (h != 0)
        {
            v = 0;
        }

        if (SpriteList != null)
        {
            if (h > 0)
            {
                tank.sprite = SpriteList[3];
                OffsetRotation = Vector3.zero;
                
            }
            else if (h < 0)
            {
                tank.sprite = SpriteList[1];
                OffsetRotation = new Vector3(0, 0, 180);
            }
            else if (v < 0)
            {
                tank.sprite = SpriteList[2];
                OffsetRotation = new Vector3(0,0,270);
            }
            else if (v > 0)
            {
                tank.sprite = SpriteList[0];
                OffsetRotation = new Vector3(0,0,90);
            }
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //Debug.LogError("Space");
            this.CreateMissile();
        }
        transform.Translate(this.MoveSpeed * new Vector3(h, v, 0) * Time.deltaTime, Space.World);
    }
    //public void Awake()
    //{
    //    Debug.LogError("1");
    //}
    //public void OnEnable()
    //{
    //    Debug.LogError("2");
    //}
    // Use this for initialization
    // void Start()
    //{

    //}

    // Update is called once per frame
    private void FixedUpdate()
    {
        this.MyMethod();
    }
    void Update ()
    {
        //Value Range（-1 1）
        MyMethod();
    }
}
